using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using System.Data;
using System.Reflection;
using System.Drawing;

//using P2.SimpleAccountingBO.Helper;
//using P2.SimpleAccounting.DataAccess;
//using P2.SimpleAccountingBO.DAL;

using P2.Framework.Helper;
using P2.Framework.Helper.DAL;



namespace P2.Framework.Helper
{
   public class P2GUIValidationSet
    {

       private DataTable _ValidationDT = new DataTable();
       private DataTable _MandatoryDT = new DataTable();

       public P2GUIValidationSet(string ValidationObjectName)
        {
            try
            {
                //Get all fields from the DB first, then validate with the GUI
                this._ValidationDT = P2.Framework.Helper.DAL.ValidationDAL.GetAllValidationObjectFields(ValidationObjectName);
                this._MandatoryDT = P2.Framework.Helper.DAL.ValidationDAL.GetObjectMandatoryFields(ValidationObjectName); 
            }
            catch (Exception ex)
            {
                P2UIExceptionHandler.handleException(ex, "GUIValidationSet", MethodInfo.GetCurrentMethod().Name.ToString(), true, true, "");
            }
        }



       public P2TransactionMessage ValidateGUIField(string FieldName, Object FieldContent)
       {
           try
           {
               P2TransactionMessage tMsg = new P2TransactionMessage("green", "Validation_OK");
               DataView view01 = new DataView(this._ValidationDT, "FieldName = '" + FieldName + "' AND ValidationType = 'Red'", "FieldName", DataViewRowState.CurrentRows);

               if (view01.Count > 0)
               {
                   foreach (DataRowView dr in view01)
                   {
                       switch (dr["FieldType"].ToString())
                       {                           
                           case "DateTime":
                               DateTime xDateTime;
                               if (DateTime.TryParse(FieldContent.ToString(), out xDateTime))                               
                               {//Contiune
                                   if (Convert.ToDateTime(FieldContent) <= Convert.ToDateTime(dr["MaxDateTime"]) && Convert.ToDateTime(FieldContent) >= Convert.ToDateTime(dr["MinDateTime"]))
                                   {
                                       tMsg.TransactionStatus = "green";
                                       tMsg.MessageText = "Validation_OK";
                                   }
                                   else
                                   {
                                       tMsg.TransactionStatus = "red";
                                       tMsg.MessageText = "Validation_NotOK_CheckPeriod";
                                   }
                               }
                               else
                               {
                                   tMsg.TransactionStatus = "red";
                                   tMsg.MessageText = "Validation_NotOK_CheckTypeDateTime";
                               }
                               break;


                           case "Int32": //To be done: check how to check the convertion before converting                               
                               int xInt;
                               if (Int32.TryParse(FieldContent.ToString(), out xInt))
                               {//Contiune
                                   
                                   if (Convert.ToInt32(FieldContent) <= Convert.ToInt32(dr["MaxInt"]) && Convert.ToInt32(FieldContent) >= Convert.ToInt32(dr["MinInt"]))
                                   {
                                       tMsg.TransactionStatus = "green";
                                       tMsg.MessageText = "Validation_OK";
                                   }
                                   else
                                   {
                                       tMsg.TransactionStatus = "red";
                                       tMsg.MessageText = "Validation_NotOK_CheckLength";
                                   }
                               }
                               else
                               {
                                   tMsg.TransactionStatus = "red";
                                   tMsg.MessageText = "Validation_NotOK_CheckTypeInt";
                               }
                               break;


                           case "Double":
                               double xDbl;
                               if (Double.TryParse(FieldContent.ToString(), out xDbl))
                               {//Contiune
                                   if (Convert.ToDouble(FieldContent) <= Convert.ToDouble(dr["MaxInt"]) && Convert.ToDouble(FieldContent) >= Convert.ToDouble(dr["MinInt"]))
                                   {
                                       tMsg.TransactionStatus = "green";
                                       tMsg.MessageText = "Validation_OK";
                                   }
                                   else
                                   {
                                       tMsg.TransactionStatus = "red";
                                       tMsg.MessageText = "Validation_NotOK_CheckLength";
                                   }
                               }
                               else
                               {
                                   tMsg.TransactionStatus = "red";
                                   tMsg.MessageText = "Validation_NotOK_CheckTypeDouble";
                               }
                               break;


                           case "Text":

                               if (FieldContent.GetType() == typeof(string))
                               {//Contiune
                                   if (FieldContent.ToString().Length <= Convert.ToInt32(dr["MaxText"]) && FieldContent.ToString().Length >= Convert.ToInt32(dr["MinText"]))
                                   {
                                       tMsg.TransactionStatus = "green";
                                       tMsg.MessageText = "Validation_OK";
                                   }
                                   else
                                   {
                                       tMsg.TransactionStatus = "red";
                                       tMsg.MessageText = "Validation_NotOK_CheckLength";
                                   }
                               }
                               else
                               {
                                   tMsg.TransactionStatus = "red";
                                   tMsg.MessageText = "Validation_NotOK_CheckTypeText";
                               }
                               break;

                           default:
                               {
                                   tMsg.TransactionStatus = "green";
                                   tMsg.MessageText = "Validation_OK";
                               }
                               break;
                       }

                       //only if the upper checks went OK, not to overwrite the tMsg
                       if (tMsg.TransactionStatus == "green")
                           if (dr["Regex"].ToString() != "") //and it is set for this field
                               if (Regex.IsMatch(FieldContent.ToString(), dr["Regex"].ToString ()))
                               {
                                   tMsg.TransactionStatus = "green";
                                   tMsg.MessageText = "Validation_OK";
                               }
                               else
                               {
                                   tMsg.TransactionStatus = "red";
                                   tMsg.MessageText = "Validation_NotOK_RegEx";
                               }                      

                   }
               }
               else
               {
                   tMsg.TransactionStatus = "green";
                   tMsg.MessageText = "Validation_OK";
               }

               return tMsg;

           }
           catch (Exception ex)
           {
               P2UIExceptionHandler.handleException(ex, "GUIValidation", MethodInfo.GetCurrentMethod().Name.ToString(), true, true, "");
               P2TransactionMessage tMsgError = new P2TransactionMessage("red", "Error_Error");
               return tMsgError;
           }
       }




       //GetObjectMandatoryFields
       public List<P2TransactionMessage> ValidateMandatoryGUIField(Control.ControlCollection cc)
       {
           List<P2TransactionMessage> MessageStack = new List<P2TransactionMessage>();

           try
           {
               //P2TransactionMessage tMsg = new P2TransactionMessage("green", "Validation_OK");
               //DataView view01 = new DataView(this._MandatoryDT, "FieldName = '" + FieldName + "'", "FieldName", DataViewRowState.CurrentRows);
               
               if (_MandatoryDT.Rows.Count > 0)
               {
                   foreach (DataRow dr in _MandatoryDT.Rows) //go through each DR from the Validation Mantatory Table
                   {                       
                       foreach (Control ctr in cc) //Search through the controls of this GUI and find the Mandatory field
                       {
                           if (ctr.Name == dr["FieldName"].ToString ())                               
                               if (ctr.Text.ToString() == "") //if field is empty --> Red. Else --> do nothing, status is already green!
                                   MessageStack.Add(new P2TransactionMessage("red", "Validation_NotOK_MissingMandatoryField"));                           
                       }

                       if (MessageStack.Count == 0)                       
                           MessageStack.Add(new P2TransactionMessage("green", "Validation_OK"));                       
                   }
               }

               return MessageStack;

           }
           catch (Exception ex)
           {
               P2UIExceptionHandler.handleException(ex, "GUIValidation", MethodInfo.GetCurrentMethod().Name.ToString(), true, true, "");
               MessageStack.Add( new P2TransactionMessage("red", "Error_Error"));
               return MessageStack;
           }
       }


        //Validation on RegEx! IS not in use!
       public bool CheckRegEx(string strToBeChecked, string strFieldType)
        {
            try
            {
                switch (strFieldType)
                {
                    case "SxText":
                        if (!Regex.IsMatch(strToBeChecked, "^[a-zA-Z'.]{1,40}$")) //Must allow spaces
                            return false;
                        else
                            return true;

                    case "SxText40":
                        if (!Regex.IsMatch(strToBeChecked, "^[a-zA-Z'.]{1,40}$")) //Must allow spaces
                            return false;
                        else
                            return true;

                    case "SxInt":
                        if (!Regex.IsMatch(strToBeChecked, "^[0-9]{1,10}$"))
                            return false;
                        else
                            return true;

                    case "SxCalcS":
                        if (!Regex.IsMatch(strToBeChecked, "^[0-9]{1,10}$"))
                            return false;
                        else
                            return true;

                    case "SxCalcE":
                        if (!Regex.IsMatch(strToBeChecked, "^[0-9]{1,10}$"))
                            return false;
                        else
                            return true;

                    default:
                        return false;
                }

            }
            catch (Exception ex)
            {
                P2UIExceptionHandler.handleException(ex, "GUIValidation", MethodInfo.GetCurrentMethod().Name.ToString(), true, true, "");
                return false;
            }
        }

    }
}
